package com.jcl.cloud.services.crawler.catchnew.task;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jcl.cloud.common.service.util.HttpClientUtil;
import com.jcl.cloud.common.service.util.PageOps;
import com.jcl.cloud.services.crawler.catchnew.constant.TaskInfo;
import com.jcl.cloud.services.crawler.catchnew.model.CatchNewConfigModel;
import com.jcl.cloud.services.crawler.catchnew.service.RedisService;
import com.jcl.cloud.services.crawler.catchnew.util.*;
import javafx.concurrent.Task;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ZlzpCatchNewHandler implements CatchNewHandler {

    private final static Logger logger = LoggerFactory.getLogger(ZlzpCatchNewHandler.class);

    @Autowired
    private RedisService redisService;


    @Override
    public void execute(String crawlerId) {

        CatchNewConfigModel catchNewConfigModel = null;
        try {
            String catchNewConfig = this.redisService.hget("unicrawler:task:info:"+crawlerId,"catch-new-config");


            if(StringUtils.isBlank(catchNewConfig) || "null".equalsIgnoreCase(catchNewConfig)){
                catchNewConfigModel = new CatchNewConfigModel();
                String totalPageNo = this.getTotalPageNum(crawlerId);
                catchNewConfigModel.setNextPageNo(1);
                catchNewConfigModel.setTotalPageNo(Integer.valueOf(totalPageNo));
                this.getResumeNo(catchNewConfigModel,crawlerId);
                this.resumeIdToDetail(crawlerId);

            }else {

                catchNewConfigModel = JSON.toJavaObject(JSON.parseObject(catchNewConfig),CatchNewConfigModel.class);
                //判断totalPageNo和nextPageNo的关系
                int totalPageNo = catchNewConfigModel.getTotalPageNo();
                int nextPageNo = catchNewConfigModel.getNextPageNo();

                //判断一下队列中的任务是否为空
                long len = this.redisService.llen("crawler:brain:catch-new:tasks:"+crawlerId);
                if(len == 0){

                    if(nextPageNo+1 > totalPageNo){
                        catchNewConfigModel.setTotalPageNo(Integer.valueOf(this.getTotalPageNum(crawlerId)));
                        catchNewConfigModel.setNextPageNo(1);
                    }

                    this.getResumeNo(catchNewConfigModel,crawlerId);
                }



                this.resumeIdToDetail(crawlerId);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } finally {
            long currentTime = System.currentTimeMillis();
            long nextExecuteTime = currentTime + 20000 ;
            this.redisService.hset("unicrawler:task:info:"+crawlerId,"nextExecuteTime",String.valueOf(nextExecuteTime));

            //更新catch-new-config
            catchNewConfigModel.setNextPageNo(catchNewConfigModel.getNextPageNo() + 1);
            this.redisService.hset("unicrawler:task:info:"+crawlerId,"catch-new-config",JSON.toJSONString(catchNewConfigModel));
            //释放任务
            this.redisService.lpush("unicrawler:tasks:all", crawlerId);
        }
    }



    private String getTotalPageNum (String crawlerId){

        int pageNum = 0;
        try {
            String cookieStoreStr = this.redisService.hget("unicrawler:task:info:"+crawlerId, "cookieStore");

            BasicCookieStore cookieStore = SerializeUtils.serializeToObj(cookieStoreStr);
            HttpClientContext context = HttpClientContext.create();
            context.setCookieStore(cookieStore);

            String zp_route_meta = "";


            List<Cookie> cookieList = cookieStore.getCookies();
            for (Cookie c : cookieList) {
                if (c.getName().equals("zp-route-meta")) {
                    zp_route_meta = c.getValue();
                    break;
                }
            }


            String Cookie = "";
            for (Cookie c : cookieList) {
                Cookie = Cookie + "; " + c.getName() + "=" + c.getValue();
            }
            Map<String, String> map = new HashMap<>();
            map.put("zp_route_meta", zp_route_meta);
            map.put("Cookie", Cookie.substring(1));


            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
            String endDate = sdf.format(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, -1);
            //calendar.add(Calendar.DATE,-7);
            Date newDate = calendar.getTime();
            String startDate = sdf.format(newDate);
            String S_CreateDate = startDate + "," + endDate;
            JSONObject jo = new JSONObject();
            jo.fluentPut("S_CreateDate", S_CreateDate);
            jo.fluentPut("S_feedback", "");
            jo.fluentPut("isNewList",false);

            StringEntity se = new StringEntity(jo.toString());


            //获取总页数
            String result = PageOps.getListCounts(map, context, se);
            logger.info("getListCounts返回的结果：" + result);
            JSONArray countsArr = JSONObject.parseObject(result).getJSONObject("data").getJSONArray("counts");
            String countSum = "";
            for (int i = 0; i < countsArr.size(); i++) {
                JSONObject jsonObject = countsArr.getJSONObject(i);
                if (jsonObject.get("resumeState").equals("1")) {
                    countSum = jsonObject.getString("count");
                    break;
                }
            }

            pageNum = Integer.parseInt(countSum) / 50 ;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return  String.valueOf(pageNum);
    }




    private void  getResumeNo(CatchNewConfigModel catchNewConfigModel,String crawlerId){


        try {
            String cookieStoreStr = this.redisService.hget("unicrawler:task:info:"+crawlerId, "cookieStore");

            BasicCookieStore cookieStore = SerializeUtils.serializeToObj(cookieStoreStr);
            HttpClientContext context = HttpClientContext.create();
            context.setCookieStore(cookieStore);

            String zp_route_meta = "";


            List<Cookie> cookieList = cookieStore.getCookies();
            for (Cookie c : cookieList) {
                if (c.getName().equals("zp-route-meta")) {
                    zp_route_meta = c.getValue();
                    break;
                }
            }


            String Cookie = "";
            for (Cookie c : cookieList) {
                Cookie = Cookie + "; " + c.getName() + "=" + c.getValue();
            }
            Map<String, String> map = new HashMap<>();
            map.put("zp_route_meta", zp_route_meta);
            map.put("Cookie", Cookie.substring(1));


            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
            String endDate = sdf.format(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, -1);
            //calendar.add(Calendar.DATE,-7);
            Date newDate = calendar.getTime();
            String startDate = sdf.format(newDate);
            String S_CreateDate = startDate + "," + endDate;
             //jo = new JSONObject();
            /*jo.fluentPut("S_CreateDate", S_CreateDate);
            jo.fluentPut("S_feedback", "");*/




            JSONObject jo = new JSONObject();
            jo.fluentPut("isNewList",false);
            jo.fluentPut("S_ResumeState", "1");
            jo.fluentPut("S_CreateDate", S_CreateDate);
            jo.fluentPut("S_feedback", "");
            jo.fluentPut("page", catchNewConfigModel.getNextPageNo());
            jo.fluentPut("pageSize", "20");

            StringEntity se = new StringEntity(jo.toJSONString());
            //se.setContentType("UTF-8");

            //循环获取每页中的用户ID
            String result = PageOps.getResumeList(map, context, se);
            logger.info("getResumeList返回的结果：" + result);
            JSONArray dataList = JSONObject.parseObject(result).getJSONObject("data").getJSONArray("dataList");

            for(int i =0 ;i<dataList.size();i++){
                JSONObject jsonObject = dataList.getJSONObject(i);
                String id = jsonObject.getString("id");

                boolean hasRead = jsonObject.getBoolean("hasRead");
                if (hasRead) {
                    logger.info(String.format("*****简历id=%s 的求职者的简历已经被阅读过了*****", id));
                }

                if(StringUtils.isNotBlank(id)){
                    this.redisService.lpush("crawler:brain:catch-new:tasks:"+crawlerId,id);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {

            //判断是否总页数已经爬取完毕

        }
    }





    private void resumeIdToDetail(String crawlerId){

        String cookieStoreStr = this.redisService.hget("unicrawler:task:info:"+crawlerId, "cookieStore");

        BasicCookieStore cookieStore = SerializeUtils.serializeToObj(cookieStoreStr);
        HttpClientContext context = HttpClientContext.create();
        context.setCookieStore(cookieStore);

        String zp_route_meta = "";


        List<Cookie> cookieList = cookieStore.getCookies();
        for (Cookie c : cookieList) {
            if (c.getName().equals("zp-route-meta")) {
                zp_route_meta = c.getValue();
                break;
            }
        }


        String Cookie = "";
        for (Cookie c : cookieList) {
            Cookie = Cookie + "; " + c.getName() + "=" + c.getValue();
        }
        Map<String, String> map = new HashMap<>();
        map.put("zp_route_meta", zp_route_meta);
        map.put("Cookie", Cookie.substring(1));

        for(int n = 0;n < 10;n++){
            try {
                String id = this.redisService.rpop("crawler:brain:catch-new:tasks:"+crawlerId);
                Thread.sleep((long) (Math.random() * 15) * 1000);
                if(StringUtils.isNotBlank(id)){
                    //https://rd5.zhaopin.com/api/rd/resume/detail?_=1537176916727&resumeNo=514516719104_1_1
                    String url = "https://rd5.zhaopin.com/api/rd/resume/detail?_=" + new Date().getTime() + "&resumeNo=" + id + "_1_1";
                    map.put("url", url);
                    String Referer = "https://rd5.zhaopin.com/resume/detail?resumeNo="+id+"_1_1&openFrom=5";
                    map.put("Referer",Referer);

                    String result = PageOps.getResumeDetail(map, context);
                    logger.info("getResumeDetail返回的结果：" + result);


                    Map fieldMap = new HashMap();
                    JSONObject resumeDetailJO = JSONObject.parseObject(result);
                    JSONObject data = resumeDetailJO.getJSONObject("data");
                    JSONObject candidateJO = data.getJSONObject("candidate");
                    JSONObject jobJO = data.getJSONObject("job");
                    JSONObject detailJO = data.getJSONObject("detail");



                    String realname = candidateJO.getString("userName");
                    fieldMap.put("realname", realname);
                    String sex = candidateJO.getString("gender");
                    fieldMap.put("sex", StringUtil.code2SexZlzp(sex));
                    String mobile = candidateJO.getString("mobilePhone");
                    fieldMap.put("mobile", mobile);

                    String workyear = candidateJO.getString("workYearsRangeId");
                    fieldMap.put("workyear", StringUtil.workYearsRangeIdToWorkYearsZlzp(workyear));


                    //翻译
                    String work_industry = detailJO.getString("CurrentIndustry");
                    fieldMap.put("work_industry", work_industry);

                    String work_last_position = detailJO.getString("CurrentJobTitle");
                    fieldMap.put("work_last_position", work_last_position);
                    String work_last_company = detailJO.getString("CurrentCompanyName");
                    fieldMap.put("work_last_company", work_last_company);
                    String edu_last_education = candidateJO.getString("educationLevel");
                    fieldMap.put("edu_last_education", StringUtil.code2EduLevel(edu_last_education));

                    String edu_last_school = detailJO.getString("GraduatedFrom");
                    fieldMap.put("edu_last_school", edu_last_school);

                    String edu_last_major = detailJO.getString("CurrentMajorName");
                    fieldMap.put("edu_last_major", edu_last_major);


                    String apply_position = jobJO.getString("jobTitle");
                    fieldMap.put("apply_position", apply_position);
                    String apply_date = candidateJO.getString("dateLastLogin");
                    fieldMap.put("apply_date", new SimpleDateFormat("yyyy-MM-dd").format(new Date(Long.valueOf(apply_date))));


                    String email = candidateJO.getString("email");
                    fieldMap.put("email", email);

                    String apply_department = "未知";
                    fieldMap.put("apply_department", apply_department);


                    String birthYear = candidateJO.getString("birthYear");
                    String birthMonth = candidateJO.getString("birthMonth");
                    String birthDay = candidateJO.getString("birthDay");


                    if (birthMonth.length() == 1) {
                        birthMonth = "0" + birthMonth;
                    }

                    if (birthDay.length() == 1) {
                        birthDay = "0" + birthDay;
                    }

                    String birthday = birthYear + "-" + birthMonth + "-" + birthDay;
                    fieldMap.put("birthday", birthday);

                    String current_city = candidateJO.getString("cityId");
                    fieldMap.put("current_city", ZlzpUtil.doCode2Name(current_city, "1"));
                    String current_address = candidateJO.getString("cityId");

                    fieldMap.put("current_address", ZlzpUtil.doCode2Name(current_address, "1"));


                    String work_detail = detailJO.getString("WorkExperience");
                    fieldMap.put("work_detail", work_detail);

                    String project_detail = detailJO.getString("ProjectExperience");
                    fieldMap.put("project_detail", project_detail);

                    String edu_detail = detailJO.getString("EducationExperience");
                    fieldMap.put("edu_detail", edu_detail);


                    String source_sub = "zhaopin";
                    fieldMap.put("source_sub", source_sub);
                    String apply_status = detailJO.getString("CurrentCareerStatus");
                    fieldMap.put("apply_status", apply_status);


                    String HuKouCityId = candidateJO.getString("HuKouCityId");
                    String HuKouCityName = candidateJO.getString("HuKouCityName");
                    String HuKouProvinceId = candidateJO.getString("HuKouProvinceId");
                    String native_place = "";
                    fieldMap.put("native_place", native_place);


                    String arrival_info = detailJO.getString("CurrentCareerStatus");
                    fieldMap.put("arrival_info", arrival_info);


                    String current_salary = detailJO.getString("CurrentSalary");
                    fieldMap.put("current_salary", current_salary);
                    String DesiredSalaryScope = detailJO.getString("DesiredSalaryScope");

                    Map<String, String> salaryMap = StringUtil.salary2MapZlzp(DesiredSalaryScope);
                    String desire_salary_start = salaryMap.get("desire_salary_start");
                    fieldMap.put("desire_salary_start", desire_salary_start);
                    String desire_salary_end = salaryMap.get("desire_salary_end");
                    fieldMap.put("desire_salary_end", desire_salary_end);



                    JSONObject jobJo = data.getJSONObject("job");
                    String apply_other_jid = jobJo.getString("jobid");
                    fieldMap.put("apply_other_jid", "zhaopin" + apply_other_jid);


                    //http://www.jucailin.com/api/resume/save
                    String retStr = HttpClientUtil.doPostJson("http://www.jucailin.com/api/resume/save", JSON.toJSONString(fieldMap));
                    String type = JSON.parseObject(retStr).getString("type");
                    if ("success".equals(type)) {
                        logger.info("上传数据成功");

                        String resume_id = String.valueOf(JSONObject.parseObject(retStr).getJSONObject("resume").getInteger("id"));
                        logger.info("智联招聘的resume_id = " + resume_id);

                        //上传工作经历
                        List<Map<String, String>> worksList = StringUtil.wd2MapZlzp(work_detail);
                        for (int i = 0; i < worksList.size(); i++) {
                            Map workMap = worksList.get(i);
                            workMap.put("resume_id", resume_id);
                            HttpClientUtil.doPostJson("http://www.jucailin.com/api/resume/worksave", JSON.toJSONString(workMap));
                        }

                        //上传教育经历
                        List<Map<String, String>> edusList = StringUtil.edu2MapZlzp(edu_detail);
                        for (int i = 0; i < edusList.size(); i++) {
                            Map eduMap = edusList.get(i);
                            eduMap.put("resume_id", resume_id);
                            HttpClientUtil.doPostJson("http://www.jucailin.com/api/resume/edusave", JSON.toJSONString(eduMap));
                        }


                        //上传项目经历
                        List<Map<String, String>> projectsList = StringUtil.pd2MapZlzp(project_detail);
                        for (int i = 0; i < projectsList.size(); i++) {
                            Map projectMap = projectsList.get(i);
                            projectMap.put("resume_id", resume_id);
                            HttpClientUtil.doPostJson("http://www.jucailin.com/api/resume/projectsave", JSON.toJSONString(projectMap));
                        }
                    }
                }

                Thread.sleep((long) (Math.random() * 10) * 1000);
            } catch (Exception e) {
                logger.info("智联招聘网站 获取单个简历详细信息发生异常：",e);
            }
        }

    }



}
